#include "StdAfx.h"
#include "Camera.h"

CCamera::CCamera(void)
{
	loadIdentity();
}
CCamera::CCamera(const double* const pCamMatrix){
	for(int i=0; i<16; i++){
		m_modelViewMatrix[i] = pCamMatrix[i];
	}
}

CCamera::~CCamera(void)
{
}

void CCamera::loadIdentity(){
	for(int i=0; i<16; i++){
		m_modelViewMatrix[i] = 0.0f;
	}
	m_modelViewMatrix[0]		= 1.0f;
	m_modelViewMatrix[5]		= 1.0f;
	m_modelViewMatrix[10]		= 1.0f;
	m_modelViewMatrix[15]		= 1.0f;
}
const double* const CCamera::getModelViewMatrix(){
	return m_modelViewMatrix;
}
const double* const CCamera::getProjectionMatrix(){
	return m_projectionMatrix;
}
const int* const CCamera::getViewport(){
	return m_viewport;
}
void CCamera::loadCurrentCam(){
	glGetDoublev(GL_MODELVIEW_MATRIX, m_modelViewMatrix);
	glGetDoublev(GL_PROJECTION_MATRIX, m_projectionMatrix);
	glGetIntegerv(GL_VIEWPORT, m_viewport);
}
CPlane	CCamera::getViewingPlane(){
	double	tx, ty, tz,
			ux, uy, uz,
			vx, vy, vz,
			nx, ny, nz;
	ux		= m_modelViewMatrix[0];
	uy		= m_modelViewMatrix[4];
	uz		= m_modelViewMatrix[8];

	vx		= m_modelViewMatrix[1];
	vy		= m_modelViewMatrix[5];
	vz		= m_modelViewMatrix[9];

	nx		= m_modelViewMatrix[2];
	ny		= m_modelViewMatrix[6];
	nz		= m_modelViewMatrix[10];

	tx		= -m_modelViewMatrix[12];
	ty		= -m_modelViewMatrix[13];
	tz		= -m_modelViewMatrix[14];

	tx		= tx*ux + ty*vx + tz*nx;
	ty		= tx*uy + ty*vy + tz*ny;
	tz		= tx*uz + ty*vz + tz*nz;
	return CPlane(CPoint3D(tx, ty, tz), CVector3D(nx, ny, nz));
}
CPoint3D		CCamera::getPosition(){
	double	tx, ty, tz,
			ux, uy, uz,
			vx, vy, vz,
			nx, ny, nz;
	ux		= m_modelViewMatrix[0];
	uy		= m_modelViewMatrix[4];
	uz		= m_modelViewMatrix[8];

	vx		= m_modelViewMatrix[1];
	vy		= m_modelViewMatrix[5];
	vz		= m_modelViewMatrix[9];

	nx		= m_modelViewMatrix[2];
	ny		= m_modelViewMatrix[6];
	nz		= m_modelViewMatrix[10];

	tx		= -m_modelViewMatrix[12];
	ty		= -m_modelViewMatrix[13];
	tz		= -m_modelViewMatrix[14];

	tx		= tx*ux + ty*vx + tz*nx;
	ty		= tx*uy + ty*vy + tz*ny;
	tz		= tx*uz + ty*vz + tz*nz;
	return CPoint3D(tx, ty, tz);
}
CVector3D		CCamera::getVPNVector(){
	return CVector3D(
		m_modelViewMatrix[2],
		m_modelViewMatrix[6],
		m_modelViewMatrix[10]);

}
CVector3D		CCamera::getRightVector(){
	return CVector3D(
		m_modelViewMatrix[0],
		m_modelViewMatrix[4],
		m_modelViewMatrix[8]);

}
CVector3D		CCamera::getUpVector(){
	return CVector3D(
		m_modelViewMatrix[1],
		m_modelViewMatrix[5],
		m_modelViewMatrix[9]);


}

